package com.example.fashion.android.util.ImageUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.YuvImage;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.view.View;
import android.widget.ImageView;

import com.example.fashion.android.R;
import com.example.fashion.android.util.LogUtil.L;
import com.example.fashion.android.util.StreamUtil.StreamUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by fashion on 2017/10/13.
 */

public class ImageUtil {
    private static final String TAG = "ImageUtil";
    public static final int CUTIMG = 0;
    public static final int SCALEIMG = 1;
    public static final int ORIGINALIMG = 2;
    public static final int MAX_WIDTH = 2048;
    public static final int MAX_HEIGHT = 2048;

    public ImageUtil() {
    }

    public static Bitmap getBitmap(File file) {
        Bitmap resizeBmp = null;

        try {
            resizeBmp = BitmapFactory.decodeFile(file.getPath());
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return resizeBmp;
    }

    public static Bitmap getBitmap(String url) {
        return getBitmap((String) url, -1, -1);
    }

    public static Bitmap getBitmap(String url, int desiredWidth, int desiredHeight) {
        Bitmap resizeBmp = null;
        URLConnection con = null;
        InputStream is = null;

        try {
            URL e = new URL(url);
            con = e.openConnection();
            con.setDoInput(true);
            con.connect();
            is = con.getInputStream();
            resizeBmp = getBitmap(is, desiredWidth, desiredHeight);
        } catch (Exception var15) {
            var15.printStackTrace();
            L.d(var15.getMessage());
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception var14) {
                var14.printStackTrace();
            }

        }

        return resizeBmp;
    }

    public static Bitmap getBitmap(InputStream inputStream, int desiredWidth, int desiredHeight) {
        Bitmap resizeBmp = null;

        try {
            if (desiredWidth > 0 && desiredHeight > 0) {
                byte[] e1 = StreamUtil.stream2Bytes(inputStream);
                resizeBmp = getBitmap(e1, desiredWidth, desiredHeight);
            } else {
                BitmapFactory.Options e = new BitmapFactory.Options();
                e.inPreferredConfig = Bitmap.Config.RGB_565;
                e.inPurgeable = true;
                e.inInputShareable = true;
                e.inSampleSize = 1;
                e.inJustDecodeBounds = false;
                e.inDither = false;
                resizeBmp = BitmapFactory.decodeStream(inputStream, (Rect) null, e);
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            L.d(var5.getMessage());
        }

        return resizeBmp;
    }

    public static Bitmap getBitmap(byte[] data, int desiredWidth, int desiredHeight) {
        Bitmap resizeBmp = null;

        try {
            BitmapFactory.Options e = new BitmapFactory.Options();
            e.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(data, 0, data.length, e);
            int srcWidth = e.outWidth;
            int srcHeight = e.outHeight;
            int[] size = resizeToMaxSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
            desiredWidth = size[0];
            desiredHeight = size[1];
            e.inPreferredConfig = Bitmap.Config.RGB_565;
            e.inPurgeable = true;
            e.inInputShareable = true;
            int sampleSize = computeSampleSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
            e.inSampleSize = sampleSize;
            e.inJustDecodeBounds = false;
            e.inDither = false;
            resizeBmp = BitmapFactory.decodeByteArray(data, 0, data.length, e);
            float scale = getMinScale(resizeBmp.getWidth(), resizeBmp.getHeight(), desiredWidth, desiredHeight);
            if (scale < 1.0F) {
                resizeBmp = scaleBitmap(resizeBmp, scale);
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            L.d(var10.getMessage());
        }

        return resizeBmp;
    }

    public static Bitmap getScaleBitmap(File file, int desiredWidth, int desiredHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        int[] size = getBitmapSize(file);
        int srcWidth = size[0];
        int srcHeight = size[1];
        int[] sizeNew = resizeToMaxSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
        desiredWidth = sizeNew[0];
        desiredHeight = sizeNew[1];
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        int sampleSize = computeSampleSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
        opts.inSampleSize = sampleSize;
        opts.inJustDecodeBounds = false;
        opts.inDither = false;
        Bitmap resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
        float scale = getMinScale(resizeBmp.getWidth(), resizeBmp.getHeight(), desiredWidth, desiredHeight);
        if (scale < 1.0F) {
            resizeBmp = scaleBitmap(resizeBmp, scale);
        }

        return resizeBmp;
    }

    public static Bitmap getThumbnail(File file, int desiredWidth, int desiredHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        int[] size = getBitmapSize(file);
        int srcWidth = size[0];
        int srcHeight = size[1];
        int[] sizeNew = resizeToMaxSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
        desiredWidth = sizeNew[0];
        desiredHeight = sizeNew[1];
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        double wr = (double) srcWidth / (double) desiredWidth;
        double hr = (double) srcHeight / (double) desiredHeight;
        double ratio = Math.min(wr, hr);
        double ratioMax = 1.0D;

        float n;
        for (n = 1.0F; ratioMax * 2.0D <= ratio; ratioMax *= 2.0D) {
            ;
        }

        for (ratioMax *= 2.0D; (double) (n * 2.0F) <= ratioMax; n *= 2.0F) {
            ;
        }

        opts.inSampleSize = (int) n;
        opts.inJustDecodeBounds = false;
        opts.inDither = false;
        Bitmap resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
        return resizeBmp;
    }

    public static Bitmap getScaleBitmap(Bitmap bitmap, int desiredWidth, int desiredHeight) {
        if (!checkBitmap(bitmap)) {
            return null;
        } else {
            int srcWidth = bitmap.getWidth();
            int srcHeight = bitmap.getHeight();
            int[] size = resizeToMaxSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
            desiredWidth = size[0];
            desiredHeight = size[1];
            Bitmap resizeBmp = null;
            float scale = getMinScale(srcWidth, srcHeight, desiredWidth, desiredHeight);
            if (scale < 1.0F) {
                resizeBmp = scaleBitmap(bitmap, scale);
            }

            return resizeBmp;
        }
    }

    public static Bitmap getCutBitmap(File file, int desiredWidth, int desiredHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        int[] size = getBitmapSize(file);
        int srcWidth = size[0];
        int srcHeight = size[1];
        int[] sizeNew = resizeToMaxSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
        desiredWidth = sizeNew[0];
        desiredHeight = sizeNew[1];
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        int sampleSize = computeSampleSize(srcWidth, srcHeight, desiredWidth, desiredHeight);
        opts.inSampleSize = sampleSize;
        opts.inJustDecodeBounds = false;
        opts.inDither = false;
        Bitmap resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
        float scale = getMinScale(resizeBmp.getWidth(), resizeBmp.getHeight(), desiredWidth, desiredHeight);
        resizeBmp = scaleBitmap(resizeBmp, scale);
        if (resizeBmp.getWidth() > desiredWidth || resizeBmp.getHeight() > desiredHeight) {
            resizeBmp = getCutBitmap(resizeBmp, desiredWidth, desiredHeight);
        }

        return resizeBmp;
    }

    /**
     * 剪切图片（只有图片本身的宽高大于要剪切的宽高才回去剪切，取图片中心部分）
     * @param bitmap
     * @param desiredWidth
     * @param desiredHeight
     * @return
     */
    public static Bitmap getCutBitmap(Bitmap bitmap, int desiredWidth, int desiredHeight) {
        if (!checkBitmap(bitmap)) {
            return null;
        } else if (!checkSize(desiredWidth, desiredHeight)) {
            return null;
        } else {
            Bitmap resizeBmp = null;

            try {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int offsetX = 0;
                int offsetY = 0;
                if (width > desiredWidth) {
                    offsetX = (width - desiredWidth) / 2;
                } else {
                    desiredWidth = width;
                }

                if (height > desiredHeight) {
                    offsetY = (height - desiredHeight) / 2;
                } else {
                    desiredHeight = height;
                }

                resizeBmp = Bitmap.createBitmap(bitmap, offsetX, offsetY, desiredWidth, desiredHeight);
            } catch (Exception var11) {
                var11.printStackTrace();
            } finally {
                if (resizeBmp != bitmap) {
                    bitmap.recycle();
                }

            }

            return resizeBmp;
        }
    }

    public static Bitmap scaleBitmap(Bitmap bitmap, float scale) {
        if (!checkBitmap(bitmap)) {
            return null;
        } else if (scale == 1.0F) {
            return bitmap;
        } else {
            Bitmap resizeBmp = null;

            try {
                int e = bitmap.getWidth();
                int bmpH = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.postScale(scale, scale);
                resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, e, bmpH, matrix, true);
            } catch (Exception var9) {
                var9.printStackTrace();
            } finally {
                if (resizeBmp != bitmap) {
                    bitmap.recycle();
                }

            }

            return resizeBmp;
        }
    }

    public static int[] getBitmapSize(File file) {
        int[] size = new int[2];
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), opts);
        size[0] = opts.outWidth;
        size[1] = opts.outHeight;
        return size;
    }

    private static float getMinScale(int srcWidth, int srcHeight, int desiredWidth, int desiredHeight) {
        if (desiredWidth > 0 && desiredHeight > 0) {
            float scale = 0.0F;
            float scaleWidth = (float) desiredWidth / (float) srcWidth;
            float scaleHeight = (float) desiredHeight / (float) srcHeight;
            if (scaleWidth > scaleHeight) {
                scale = scaleWidth;
            } else {
                scale = scaleHeight;
            }

            return scale;
        } else {
            return 1.0F;
        }
    }

    private static int[] resizeToMaxSize(int srcWidth, int srcHeight, int desiredWidth, int desiredHeight) {
        int[] size = new int[2];
        if (desiredWidth <= 0) {
            desiredWidth = srcWidth;
        }

        if (desiredHeight <= 0) {
            desiredHeight = srcHeight;
        }

        float scaleHeight;
        if (desiredWidth > 2048) {
            desiredWidth = 2048;
            scaleHeight = (float) desiredWidth / (float) srcWidth;
            desiredHeight = (int) ((float) desiredHeight * scaleHeight);
        }

        if (desiredHeight > 2048) {
            desiredHeight = 2048;
            scaleHeight = (float) desiredHeight / (float) srcHeight;
            desiredWidth = (int) ((float) desiredWidth * scaleHeight);
        }

        size[0] = desiredWidth;
        size[1] = desiredHeight;
        return size;
    }

    private static boolean checkBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            L.e("原图Bitmap为空了");
            return false;
        } else if (bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
            return true;
        } else {
            L.e("原图Bitmap大小为0");
            return false;
        }
    }

    private static boolean checkSize(int desiredWidth, int desiredHeight) {
        if (desiredWidth > 0 && desiredHeight > 0) {
            return true;
        } else {
            L.e("请求Bitmap的宽高参数必须大于0");
            return false;
        }
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), (drawable.getOpacity() != PixelFormat.OPAQUE) ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    public static Drawable bitmapToDrawable(Bitmap bitmap) {
        BitmapDrawable mBitmapDrawable = null;

        try {
            if (bitmap == null) {
                return null;
            }

            mBitmapDrawable = new BitmapDrawable(bitmap);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return mBitmapDrawable;
    }

    public static TransitionDrawable bitmapToTransitionDrawable(Bitmap bitmap) {
        TransitionDrawable mBitmapDrawable = null;

        try {
            if (bitmap == null) {
                return null;
            }

            mBitmapDrawable = new TransitionDrawable(new Drawable[]{new ColorDrawable(0), new BitmapDrawable(bitmap)});
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return mBitmapDrawable;
    }

    public static TransitionDrawable drawableToTransitionDrawable(Drawable drawable) {
        TransitionDrawable mBitmapDrawable = null;

        try {
            if (drawable == null) {
                return null;
            }

            mBitmapDrawable = new TransitionDrawable(new Drawable[]{new ColorDrawable(0), drawable});
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return mBitmapDrawable;
    }

    public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, boolean needRecycle) {
        byte[] result = null;
        ByteArrayOutputStream output = null;

        try {
            output = new ByteArrayOutputStream();
            bitmap.compress(mCompressFormat, 70, output);
            result = output.toByteArray();
            if (needRecycle) {
                bitmap.recycle();
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (Exception var13) {
                    var13.printStackTrace();
                }
            }

        }

        return result;
    }

    public static int getByteCount(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat) {
        int size = 0;
        ByteArrayOutputStream output = null;

        try {
            output = new ByteArrayOutputStream();
            bitmap.compress(mCompressFormat, 70, output);
            byte[] e = output.toByteArray();
            size = e.length;
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (Exception var12) {
                    var12.printStackTrace();
                }
            }

        }

        return size;
    }

    public static Bitmap bytes2Bimap(byte[] b) {
        Bitmap bitmap = null;

        try {
            if (b.length != 0) {
                bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return bitmap;
    }

    public static Bitmap imageView2Bitmap(ImageView view) {
        Bitmap bitmap = null;

        try {
            bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return bitmap;
    }

    public static Drawable view2Drawable(View view) {
        BitmapDrawable mBitmapDrawable = null;

        try {
            Bitmap e = view2Bitmap(view);
            if (e != null) {
                mBitmapDrawable = new BitmapDrawable(e);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return mBitmapDrawable;
    }

    public static Bitmap view2Bitmap(View view) {
        Bitmap bitmap = null;

        try {
            if (view != null) {
                view.setDrawingCacheEnabled(true);
                view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
                view.buildDrawingCache();
                bitmap = view.getDrawingCache();
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return bitmap;
    }

    public static byte[] view2Bytes(View view, Bitmap.CompressFormat compressFormat) {
        byte[] b = null;

        try {
            Bitmap e = view2Bitmap(view);
            b = bitmap2Bytes(e, compressFormat, true);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return b;
    }

    public static Bitmap rotateBitmap(Bitmap bitmap, float degrees) {
        Bitmap mBitmap = null;

        try {
            Matrix e = new Matrix();
            e.setRotate(degrees % 360.0F);
            mBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), e, false);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return mBitmap;
    }

    public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
        Object mBitmap = null;

        try {
            Matrix e = new Matrix();
            int width;
            int height;
            if (degrees / 90.0F % 2.0F != 0.0F) {
                width = bitmap.getWidth();
                height = bitmap.getHeight();
            } else {
                width = bitmap.getHeight();
                height = bitmap.getWidth();
            }

            int cx = width / 2;
            int cy = height / 2;
            e.preTranslate((float) (-cx), (float) (-cy));
            e.postRotate(degrees);
            e.postTranslate((float) cx, (float) cy);
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return (Bitmap) mBitmap;
    }

    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        } else {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float roundPx;
            float left;
            float top;
            float right;
            float bottom;
            float dst_left;
            float dst_top;
            float dst_right;
            float dst_bottom;
            if (width <= height) {
                roundPx = (float) (width / 2);
                top = 0.0F;
                bottom = (float) width;
                left = 0.0F;
                right = (float) width;
                height = width;
                dst_left = 0.0F;
                dst_top = 0.0F;
                dst_right = (float) width;
                dst_bottom = (float) width;
            } else {
                roundPx = (float) (height / 2);
                float output = (float) ((width - height) / 2);
                left = output;
                right = (float) width - output;
                top = 0.0F;
                bottom = (float) height;
                width = height;
                dst_left = 0.0F;
                dst_top = 0.0F;
                dst_right = (float) height;
                dst_bottom = (float) height;
            }

            Bitmap output1 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output1);
            Paint paint = new Paint();
            Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
            Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
            RectF rectF = new RectF(dst);
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, src, dst, paint);
            return output1;
        }
    }

    public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) {
        if (bitmap == null) {
            return null;
        } else {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float left;
            float top;
            float right;
            float bottom;
            float dst_left;
            float dst_top;
            float dst_right;
            float dst_bottom;
            if (width <= height) {
                top = 0.0F;
                bottom = (float) width;
                left = 0.0F;
                right = (float) width;
                height = width;
                dst_left = 0.0F;
                dst_top = 0.0F;
                dst_right = (float) width;
                dst_bottom = (float) width;
            } else {
                float output = (float) ((width - height) / 2);
                left = output;
                right = (float) width - output;
                top = 0.0F;
                bottom = (float) height;
                width = height;
                dst_left = 0.0F;
                dst_top = 0.0F;
                dst_right = (float) height;
                dst_bottom = (float) height;
            }

            Bitmap output1 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output1);
            Paint paint = new Paint();
            Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
            Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
            RectF rectF = new RectF(dst);
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rectF, (float) roundPx, (float) roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, src, dst, paint);
            return output1;
        }
    }

    public static Bitmap toReflectionBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        } else {
            try {
                byte e = 1;
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.preScale(1.0F, -1.0F);
                Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);
                Bitmap bitmapWithReflection = Bitmap.createBitmap(width, height + height / 2, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmapWithReflection);
                canvas.drawBitmap(bitmap, 0.0F, 0.0F, (Paint) null);
                Paint deafaultPaint = new Paint();
                canvas.drawRect(0.0F, (float) height, (float) width, (float) (height + e), deafaultPaint);
                canvas.drawBitmap(reflectionImage, 0.0F, (float) (height + e), (Paint) null);
                Paint paint = new Paint();
                LinearGradient shader = new LinearGradient(0.0F, (float) bitmap.getHeight(), 0.0F, (float) (bitmapWithReflection.getHeight() + e), 1895825407, 16777215, Shader.TileMode.CLAMP);
                paint.setShader(shader);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
                canvas.drawRect(0.0F, (float) height, (float) width, (float) (bitmapWithReflection.getHeight() + e), paint);
                bitmap = bitmapWithReflection;
            } catch (Exception var11) {
                var11.printStackTrace();
            }

            return bitmap;
        }
    }

    public static void releaseBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            try {
                if (!bitmap.isRecycled()) {
                    L.d( "Bitmap释放" + bitmap.toString());
                    bitmap.recycle();
                }
            } catch (Exception var2) {
                ;
            }

            bitmap = null;
        }

    }

    public static void releaseBitmapArray(Bitmap[] bitmaps) {
        if (bitmaps != null) {
            try {
                Bitmap[] var1 = bitmaps;
                int var2 = bitmaps.length;

                for (int var3 = 0; var3 < var2; ++var3) {
                    Bitmap bitmap = var1[var3];
                    if (bitmap != null && !bitmap.isRecycled()) {
                        L.d( "Bitmap释放" + bitmap.toString());
                        bitmap.recycle();
                    }
                }
            } catch (Exception var5) {
                ;
            }
        }

    }

    public static void releaseBitmapList(List<Bitmap> bitmaps) {
        if (bitmaps != null) {
            try {
                Iterator var1 = bitmaps.iterator();

                while (var1.hasNext()) {
                    Bitmap bitmap = (Bitmap) var1.next();
                    if (bitmap != null && !bitmap.isRecycled()) {
                        L.d( "Bitmap释放" + bitmap.toString());
                        bitmap.recycle();
                    }
                }
            } catch (Exception var3) {
                ;
            }
        }

    }

    public Bitmap yuv2GrayBitmap(byte[] yuvData, int width, int height) {
        int[] pixels = new int[width * height];
        byte[] yuv = yuvData;
        int inputOffset = 0;

        for (int bitmap = 0; bitmap < height; ++bitmap) {
            int outputOffset = bitmap * width;

            for (int x = 0; x < width; ++x) {
                int grey = yuv[inputOffset + x] & 255;
                pixels[outputOffset + x] = -16777216 | grey * 65793;
            }

            inputOffset += width;
        }

        Bitmap var11 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        var11.setPixels(pixels, 0, width, 0, 0, width, height);
        return var11;
    }

    public static Bitmap yuv2Bitmap(byte[] data, int width, int height) {
        YuvImage image = new YuvImage(data, 17, width, height, (int[]) null);
        ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
        if (!image.compressToJpeg(new Rect(0, 0, width, height), 100, os)) {
            return null;
        } else {
            byte[] tmp = os.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
            return bitmap;
        }
    }

    public static Bitmap cropYuv2Bitmap(byte[] data, int width, int height, Rect rect) {
        int w = rect.width();
        int h = rect.height();
        int frameSize = width * height;
        int[] pixels = new int[w * h];
        byte[] yuv = data;
        int yOffset = rect.top * width + rect.left;
        int uvOffset = rect.top / 2 * width + rect.left / 2 * 2 + frameSize;

        for (int bitmap = 0; bitmap < h; ++bitmap) {
            int outputOffset = bitmap * w;

            for (int j = 0; j < w; ++j) {
                int y = (255 & yuv[yOffset + j]) - 16;
                int k = j >> 1 << 1;
                int v = (255 & yuv[uvOffset + k]) - 128;
                int u = (255 & yuv[uvOffset + k + 1]) - 128;
                int y1192 = 1192 * y;
                int r = y1192 + 1634 * v;
                int g = y1192 - 833 * v - 400 * u;
                int b = y1192 + 2066 * u;
                if (r < 0) {
                    r = 0;
                } else if (r > 262143) {
                    r = 262143;
                }

                if (g < 0) {
                    g = 0;
                } else if (g > 262143) {
                    g = 262143;
                }

                if (b < 0) {
                    b = 0;
                } else if (b > 262143) {
                    b = 262143;
                }

                pixels[outputOffset + j] = -16777216 | r << 6 & 16711680 | g >> 2 & '\uff00' | b >> 10 & 255;
            }

            yOffset += width;
            if ((rect.top + bitmap & 1) == 1) {
                uvOffset += width;
            }
        }

        Bitmap var22 = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        var22.setPixels(pixels, 0, w, 0, 0, w, h);
        return var22;
    }

    private static int computeSampleSize(int width, int height, int desiredWidth, int desiredHeight) {
        double wr = (double) width / (double) desiredWidth;
        double hr = (double) height / (double) desiredHeight;
        double ratio = Math.min(wr, hr);

        float n;
        for (n = 1.0F; (double) (n * 2.0F) <= ratio; n *= 2.0F) {

        }

        return (int) n;
    }

    /**
     * 将图片按照某个角度进行旋转
     *
     * @param bm     需要旋转的图片
     * @param degree 旋转角度
     * @return 旋转后的图片
     */
    public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
        Bitmap returnBm = null;

        // 根据旋转角度，生成旋转矩阵
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        try {
            // 将原始图片按照旋转矩阵进行旋转，并得到新的图片
            returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
        if (returnBm == null) {
            returnBm = bm;
        }
        if (bm != returnBm) {
            bm.recycle();
        }
        return returnBm;
    }



}
